package cn.sola.shiro.tutotisl.util;

import java.util.ResourceBundle;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPoolUtil {

	private static JedisPool pool;
	static {
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
		if (bundle == null) {
			throw new IllegalArgumentException("[redis.properties] is not found!");
		}
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
		System.out.println(Integer.valueOf(bundle.getString("redis.pool.maxActive")));

		config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
		System.out.println(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));

		config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
		System.out.println(Long.valueOf(bundle.getString("redis.pool.maxWait")));

		config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
		System.out.println(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));

		config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
		System.out.println(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));

		pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));

		System.out.println(bundle.getString("redis.ip"));
		System.out.println(bundle.getString("redis.port"));
	}

	public static void main(String[] args) {
		Jedis jedis = pool.getResource();
		jedis.auth("redis");
		String redis = jedis.get("redis");
		System.out.println(redis);
	}

	@Test
	public void testRedisPool() {
		JedisPoolConfig config = new JedisPoolConfig();
		JedisPool jedisPool = new JedisPool(config, "192.168.43.156", 6379);
		// 从池中获取一个Jedis对象  
		Jedis jedis = jedisPool.getResource();
		jedis.auth("redis");
		String redis = jedis.get("redis");
		System.out.println(redis);
	}
}
